
BalVars = list('C.C4_Age','Male','AssetSum','C.C50_Years.Of.Schooling.',
               'C.C14_Permanent.Residence.of.Jaipur.','GenCaste', 'Hindu','SocDens','NeighDens')

test_f = function(x,c){
  t.test(Num(A[which(A$ListTreat == 1),x]),Num(A[which(A$ListTreat == c),x]))}

#for Individual Favors Treatment
Favors_Balance = llply(BalVars, function(x) test_f(x,3))
bal_tab_Favors=data.frame(cbind(unlist(lapply((lapply(Favors_Balance, '[[', 5)),'[[',1)),unlist(lapply((lapply(Favors_Balance, '[[', 5)),'[[',2)),unlist(lapply(Favors_Balance, '[[', 3))))
names(bal_tab_Favors)=c('Mean (Control)','Mean (Favors Treatment)','p')
bal_tab_Favors = rbind(bal_tab_Favors, c(sum(A$ListTreat == 1), sum(A$ListTreat == 3), NA))
rownames(bal_tab_Favors) = c('Age','Male','Assets','Education','Permanent Resident','General Caste','Hindu',
                             'Connectedness','Neigh. Density','N')
out = stargazer(round(bal_tab_Favors, 2), digits = 2, digits.extra = 2, summary=F, rownames=T, notes.append=T, header = F,
                title = 'Balance table: Individual Clientelism Treatment')
writeLines(out,con = paste0('../results/C1_',(Sys.time() %>% str_replace_all(c(":" = "-", " " = "_"))),'.tex')); rm(out)

rm(BalVars, Favors_Balance, bal_tab_Favors)